using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace DD.Data.Session
{
    public static class CartManager
    {
        public static void AddItemToCart(int pid, int sid, string did, int qty)
        {
            CheckAndCreateCart();
            Visitors.Visitor vis = new DD.Visitors.Visitor();
            DataSet ds = new DataSet();
            ds = GetCartDS();

            bool IsInCart = false;
            int highId = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ((Int32)dr["PID"] == pid)
                {
                    if ((Int32)dr["SID"] == sid)
                    {
                        UpdateQty((Int32)dr["QTY"] + qty, (Int32)dr["UNIQUE_ID"]);
                        IsInCart = true;
                    }
                }
                if((Int32)dr["UNIQUE_ID"] > highId)
                {
                    highId = (Int32)dr["UNIQUE_ID"];
                }

            }
            if (!IsInCart)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr["PID"] = pid;
                dr["SID"] = sid;
                dr["DROPSHIP_ID"] = did;
                dr["UID"] = vis.UserId;
                dr["UNIQUE_ID"] = highId + 1;
                dr["QTY"] = qty;
                ds.Tables[0].Rows.Add(dr);
                HttpContext.Current.Session["USERCART"] = ds;
            }




        }
        public static void CreateCart()
        {
            DataTable dt = new DataTable();

            DataColumn dc1 = new DataColumn("PID");
            DataColumn dc2 = new DataColumn("SID");
            DataColumn dc3 = new DataColumn("DROPSHIP_ID");
            DataColumn dc4 = new DataColumn("UID");
            DataColumn dc5 = new DataColumn("UNIQUE_ID");
            DataColumn dc6 = new DataColumn("QTY");

            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            dt.Columns.Add(dc6);

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            //ds.Tables[0] = dt;

            HttpContext.Current.Session["USERCART"] = ds;
            
        }
        public static void UpdateQty(int Quantity, int Unique_Id)
        {
            if (Quantity == 0)
            {
                RemoveItem(Unique_Id);
            }
            else
            {
                DataSet ds = new DataSet();
                ds = GetCartDS();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if ((Int32)dr["UNIQUE_ID"] == Unique_Id)
                    {
                        dr["QTY"] = Quantity;
                    }
                }
                HttpContext.Current.Session["USERCART"] = ds;
            }
        }
        public static void RemoveItem(int Unique_Id)
        {
            DataSet ds = new DataSet();
            ds = GetCartDS();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ((Int32)dr["UNIQUE_ID"] == Unique_Id)
                {
                    ds.Tables[0].Rows.Remove(dr);
                }
            }
        }
        public static DataSet GetCartDS()
        {
            CheckAndCreateCart();
            return (DataSet)HttpContext.Current.Session["USERCART"];
        }
        public static void CheckAndCreateCart()
        {
            if (HttpContext.Current.Session["USERCART"] == null)
            {
                CreateCart();
            }
        }
    }
}
